package io.adrainty.bolee.security.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.adrainty.bolee.framework.commons.basic.ResponseCode;
import io.adrainty.bolee.framework.commons.exception.AppException;
import io.adrainty.bolee.framework.commons.utils.IdentifierNoUtil;
import io.adrainty.bolee.security.models.entity.DeptEntity;
import io.adrainty.bolee.security.models.entity.PostEntity;
import io.adrainty.bolee.security.repository.IDeptRepository;
import io.adrainty.bolee.security.repository.IPostRepository;
import io.adrainty.bolee.security.service.IPostService;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author AdRainty
 * @version V1.0.0
 * @since 2025/8/12 上午12:24
 */

@Service
public class PostServiceImpl implements IPostService {

    @Resource
    private IPostRepository postRepository;

    @Resource
    private IDeptRepository deptRepository;

    @Override
    public IPage<PostEntity> findPostPage(PostEntity entity, Integer pageNum, Integer pageSize) {
        IPage<PostEntity> pageResult = postRepository.pagePost(pageNum, pageSize, entity);
        List<PostEntity> records = pageResult.getRecords();
        fillPostData(records);
        return pageResult;
    }

    @Override
    public PostEntity createPost(PostEntity entity) {
        String postNo = this.createPostNo(entity.getDeptNo());
        entity.setPostNo(postNo);
        boolean result = postRepository.createPost(entity);
        if (!result) {
            throw new AppException(ResponseCode.POST_CREATE_ERROR);
        }
        fillPostData(List.of(entity));
        return entity;
    }

    @Override
    public Boolean updatePost(PostEntity entity) {
        if (entity.getId() == null) {
            throw new AppException(ResponseCode.ILLEGAL_PARAMETER);
        }
        return postRepository.updatePost(entity);
    }

    @Override
    public List<PostEntity> listPost(PostEntity entity) {
        List<PostEntity> result = postRepository.listPost(entity);
        fillPostData(result);
        return result;
    }

    /**
     * 填充岗位数据
     * @param records 岗位列表
     */
    private void fillPostData(List<PostEntity> records) {
        if (CollUtil.isEmpty(records)) return;
        Set<String> deptNoList = records.stream().map(PostEntity::getDeptNo)
                .filter(StringUtils::isNotBlank).collect(Collectors.toSet());
        Map<String, DeptEntity> deptEntityMap = deptRepository.findDeptByNoList(deptNoList).stream()
                .collect(Collectors.toMap(DeptEntity::getDeptNo, dept -> dept));
        records.forEach(post -> post.setDeptVO(deptEntityMap.get(post.getDeptNo())));
    }

    /**
     * 创建职位编号
     *
     * @param deptNo 部门编号
     * @return 职位编号
     */
    private String createPostNo(String deptNo) {
        List<PostEntity> postEntities = postRepository.getDeptPostList(deptNo);
        if (CollUtil.isEmpty(postEntities)) {
            return IdentifierNoUtil.createNo(deptNo, false);
        } else {
            return postEntities.stream()
                    .map(dept -> Long.valueOf(dept.getPostNo()))
                    .max(Comparator.comparing(i -> i))
                    .map(String::valueOf)
                    .map(i -> IdentifierNoUtil.createNo(i, true))
                    .orElse(IdentifierNoUtil.createNo(deptNo, false));
        }
    }

}
